﻿<?xml version="1.0" encoding="utf-8" ?>
<examples>
  <item id="QuickStart#Sql">
    <sampleCode language="Sql">
CREATE TABLE Products (
    ProductID INT IDENTITY(1, 1) NOT NULL,
    ProductName VARCHAR(40) NOT NULL
    CONSTRAINT PRIMARY KEY (ProductID)
);
    </sampleCode>
  </item>
  <item id="QuickStart#PrimaryKey">
    <sampleCode language="CSharp">
using System;
using Salamanca.DataAccess;

namespace QuickStart
{

    public class ProductPrimaryKey:
      PrimaryKey&lt;int&gt;
    {

        public ProductPrimaryKey():
            base()
        {
        }

        public ProductPrimaryKey(int key):
            base(key)
        {
        }

        public static implicit operator ProductPrimaryKey(int key)
        {
            return new ProductPrimaryKey(key);
        }

        public static explicit operator int(ProductPrimaryKey key)
        {
            return key.Value;
        }
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#DataTransferObject">
    <sampleCode language="CSharp">
using System;
using Salamanca.DataAccess;

namespace QuickStart
{

    public class ProductDataTransfer:
        IDataTransferObject
    {

        public object Clone()
        {
            return MemberwiseClone();
        }

        public int Id;
        public string Name;
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#IDataMapper">
    <sampleCode language="CSharp">
using System;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;

namespace QuickStart
{

    public interface IPersonMapper:
        IDataMapper
    {
        Product Find(ProductPrimaryKey key, DataMapperCollection dataMappers);
        DomainEntityKeyedCollection&lt;Product&gt; FindAll(DataMapperCollection dataMappers);
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#DomainEntity">
    <sampleCode language="CSharp">
using System;
using System.Collections.Generic;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;

namespace QuickStart
{

    public class Product:
        DomainEntity&lt;ProductPrimaryKey, ProductDataTransfer&gt;
    {

        public Product():
            base()
        {
        }

        public Product(DataMapperCollection dataMappers):
            base(dataMappers)
        {
        }

        protected override ProductPrimaryKey CreateId()
        {
            return new ProductPrimaryKey(Data.Id);
        }

        public static Product Find(ProductPrimaryKey key, DataMapperCollection dataMappers)
        {
            return ((IProductMapper)dataMappers[typeof(Product)]).Find(key, dataMappers);
        }

        public static IList&lt;Product&gt; FindAll(DataMapperCollection dataMappers)
        {
            return ((IProductMapper)dataMappers[typeof(Product)]).FindAll(dataMappers);
        }

        internal void SetId(int id)
        {
            Data.Id=id;
        }

        [StringLengthRule(40)]
        [NotNullRule]
        public string Name
        {
            get
            {
                Load(true);
                return Data.Name;
            }
            set
            {
                if (Data.Name!=value)
                {
                    Data.Name=value;
                    RaisePropertyChanged("Name");
                }
            }
        }
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#DataMapper">
    <sampleCode language="CSharp">
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace QuickStart
{

     public partial class ProductMapper:
         DataMapper&lt;Product, ProductDataTransfer, ProductPrimaryKey&gt;,
         IProductMapper
     {
         public ProductMapper(DbConnection connection, ICacheManager cacheManager):
             base(connection, DomainEntityCreator.CreateDefault&lt;Product&gt;(), cacheManager)
         {
         }

         public DomainEntityKeyedCollection&lt;Product&gt; FindAll(DataMapperCollection dataMappers)
         {
             IDbCommand[] commands=new IDbCommand[1];

             commands[0]=Connection.CreateCommand();
             commands[0].CommandType=CommandType.Text;
             commands[0].CommandText="SELECT ProductID, ProductName FROM Products";

             return Load(commands, dataMappers);
         }

         protected override IList&lt;IDbCommand&gt; CreateSelectCommands(ProductPrimaryKey key)
         {
             IDbCommand[] commands=new IDbCommand[1];

             commands[0]=Connection.CreateCommand();
             commands[0].CommandType=CommandType.Text;
             commands[0].CommandText="SELECT ProductName FROM Products WHERE ProductID=@Id";
             IDbDataParameter p=commands[0].CreateParameter();
             p.ParameterName="@Id";
             p.DbType=DbType.Int32;
             p.Value=key.Value;
             p.Direction=ParameterDirection.Input;
             commands[0].Parameters.Add(p);

             return commands;
         }

         protected override IList&lt;IDbCommand&gt; CreateDeleteCommands(ProductDataTransfer data)
         {
             IDbCommand[] commands=new IDbCommand[1];

             commands[0]=Connection.CreateCommand();
             commands[0].CommandType=CommandType.Text;
             commands[0].CommandText="DELETE FROM Products WHERE ProductID=@Id";

             IDbDataParameter p=commands[0].CreateParameter();
             p.ParameterName="@Id";
             p.DbType=DbType.Int32;
             p.Value=data.Id;
             p.Direction=ParameterDirection.Input;
             commands[0].Parameters.Add(p);

             return commands;
         }

         protected override IList&lt;IDbCommand&gt; CreateInsertCommands(ProductDataTransfer data)
         {
             IDbCommand[] commands=new IDbCommand[1];

             commands[0]=Connection.CreateCommand();
             commands[0].CommandType=CommandType.Text;
             commands[0].CommandText="INSERT INTO Products (ProductName) VALUES(@ProductName)";

             IDbDataParameter p=commands[0].CreateParameter();
             p.ParameterName="@Name";
             p.DbType=DbType.String;
             p.Value=data.Name;
             p.Direction=ParameterDirection.Input;
             commands[0].Parameters.Add(p);

             return commands;
         }

         protected override IList&lt;IDbCommand&gt; CreateUpdateCommands(ProductDataTransfer data)
         {
             IDbCommand[] commands=new IDbCommand[1];

             commands[0]=Connection.CreateCommand();
             commands[0].CommandType=CommandType.Text;
             commands[0].CommandText="UPDATE Products SET ProductName=@Name WHERE ProductID=@Id";

             IDbDataParameter p=commands[0].CreateParameter();
             p.ParameterName="@Name";
             p.DbType=DbType.String;
             p.Value=data.Name;
             p.Direction=ParameterDirection.Input;
             commands[0].Parameters.Add(p);

             p=commands[0].CreateParameter();
             p.ParameterName="@Id";
             p.DbType=DbType.Int32;
             p.Value=data.Id;
             p.Direction=ParameterDirection.Input;
             commands[0].Parameters.Add(p);

             return commands;
         }

         protected override ProductDataTransfer GetDataTransferObject(IList&lt;IDataRecord&gt; records)
         {
             ProductDataTransfer data=new ProductDataTransfer();
             int ord=records[0].GetOrdinal("ProductID");
             if (!records[0].IsDBNull(ord))
                 data.Id=records[0].GetInt32(ord);
             ord=records[0].GetOrdinal("ProductName");
             if (!records[0].IsDBNull(ord))
                 data.Name=records[0].GetString(ord);

             return data;
         }

         protected override void OnInserted(Product domainEntity, IList&lt;IDbCommand&gt; commands)
         {
             IDbCommand command=Connection.CreateCommand();
             command.CommandType=CommandType.Text;
             command.CommandText="SELECT @@IDENTITY";
             command.Transaction=commands[0].Transaction;

             domainEntity.SetId(Convert.ToInt32(command.ExecuteScalar()));

             base.OnInserted(domainEntity, commands);
         }
     }
}
    </sampleCode>
  </item>
  <item id="QuickStart#DataMapperCollection">
    <sampleCode language="CSharp">
SqlConnection connection=new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);

DataMapperCollection dataMappers=new DataMapperCollection();
dataMappers.Add(typeof(Product), new ProductMapper(connection, new NoCacheManager()));
    </sampleCode>
  </item>
  <item id="QuickStart#DomainModelConsole">
    <sampleCode language="CSharp">
Product np=new Product(dataMappers);
Console.Write("Enter the new product name : ");
np.name=Console.ReadLine();
np.Save();

foreach (Product p in Product.FindAll(dataMappers))
    Console.WriteLine(p.Name);
    </sampleCode>
  </item>
  <item id="QuickStart#ActivityData">
    <sampleCode language="CSharp">
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Salamanca.DataActivities;

namespace QuickStart
{

    public class Data:
        ActivityData
    {

        private struct Backup
        {
            public string Name;
            public Product[] Products;
        }

        protected override object CreateBackup()
        {
            Backup ret=new Backup();
            ret.Name=_Name;
            ret.Products=_Products.ToArray();
            return ret;
        }

        protected override void RestoreBackup(object backup)
        {
            Backup b=(Backup)backup;
            _Name=b.Name;
            _Products=new List&lt;Product&gt;(b.Products);
        }

        public string Name
        {
            get { return _Name; }
            set { _Name=value; }
        }

        public IList&lt;Product&gt; Products
        {
            get { return _Products; }
            set { _Products=new List&lt;Product&gt;(value); }
        }

        private string _Name;
        private List&lt;Product&gt; _Products=new List&lt;Product&gt;();
    }
}
</sampleCode>
  </item>
  <item id="QuickStart#AbstractQuestionFactory">
    <sampleCode language="CSharp">
using System;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;

namespace QuickStart
{
    public abstract class QuestionFactory
    {
        public abstract Question AskProductName(IActivityController controller);
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#Parameters">
    <sampleCode language="CSharp">
using System;
using Salamanca.DataAccess.Collections;
using Salamanca.DataActivities;

namespace QuickStart
{

    public class Parameters:
        IQuestionFactoryParameters&lt;QuestionFactory&gt;
    {

        public Parameters(QuestionFactory factory, DataMapperCollection dataMappers)
        {
            _QuestionFactory=factory;
            _DataMappers=dataMappers;
        }

        public DataMapperCollection DataMappers
        {
            get
            {
                return _DataMappers;
            }
        }

        public QuestionFactory QuestionFactory
        {
            get
            {
                return _QuestionFactory;
            }
        }

        private DataMapperCollection _DataMappers;
        private QuestionFactory _QuestionFactory;
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#QuestionState">
    <sampleCode language="CSharp">
using System;
using System.Collections.Generic;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;
using Salamanca.DataRules;

namespace QuickStart
{

    public class FindProductListByName:
        QuestionActivityState&lt;Data&gt;,
        IActivityInitialState
    {

        public FindProductListByName(Data data, QuestionFactory factory, DataMapperCollection dataMappers):
            base(data, new Parameters(factory, dataMappers))
        {
        }

        protected override IList&lt;IRule&gt; CreateRules()
        {
            IList&lt;IRule&gt; ret=base.CreateRules();
            ret.Add(
                new PredicateRule&lt;FindProductListByName&gt;(
                    "The name must not be empty.",
                    new Predicate&lt;FindProductListByName&gt;(
                        delegate(FindProductListByName s) {
                            return !string.IsNullOrEmpty(s.Data.Name);
                        }
                    )
                )
            );
            return ret;
        }

        protected override void OnInitialized(ActivityStateEventArgs e)
        {
            Question=((Parameters)Parameters).QuestionFactory.AskProductName(e.Controller);
            base.OnInitialized(e);
        }

        protected override IActivityState NextState
        {
            get { return new FindProductListByNameFindProducts(Data, (Parameters)Parameters); }
        }
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#ActivitySate">
    <sampleCode language="CSharp">
using System;
using System.Collections.Generic;
using Salamanca.DataActivities;
using Salamanca.DataActivities.UI;
using Salamanca.DataRules;

namespace QuickStart
{

    internal class FindProductListByNameFindProducts:
        ActivityState&lt;Data&gt;
    {

        public FindProductListByNameFindProducts(Data data, Parameters parameters) :
            base(data, parameters)
        {
        }

        protected override ActivityStateResult Handle(IActivityController controller)
        {
            List&lt;Product&gt; allProducts=new List&lt;Product&gt;(Product.FindAll(((Parameters)Parameters).DataMappers));
            Data.Products=allProducts.FindAll(
                new Predicate&lt;Product&gt;(
                    delegate(Product p) {
                        return p.Name.Contains(Data.Name);
                    }
                )
            );
            return ActivityStateResult.Next;
        }

        protected override IActivityState NextState
        {
            get { return new EndActivityState&lt;Data&gt;(Data); }
        }
    }
}
    </sampleCode>
  </item>
  <item id="QuickStart#TestQuestionFactory">
    <sampleCode language="CSharp">
internal sealed class TestQuestionFactory:
    QuestionFactory
{
    public TestQuestionFactory(string name)
    {
        _Name=name;
    }
    public override Question AskProductName(IActivityController controller)
    {
        Question ret=new AnsweredQuestion();
        ret.Answering+=new EventHandler&lt;AnswerEventArgs&gt;(
            delegate(object sender, AnswerEventArgs e) {
                ((Data)e.Data).Name=_Name;
            }
        );
        return ret;
    }
    private string _Name;
}
    </sampleCode>
  </item>
  <item id="QuickStart#TestActivity">
    <sampleCode language="CSharp">
[TestMethod]
public void CanFindProductList()
{
    Data data=new Data();
    ActivityController controller=new ActivityController(
        new FindProductListByName(
            data,
            new TestQuestionFactory("Queso"),
            DataMappers
        )
    );
    controller.Execute();
    Assert.IsTrue(controller.HasCompleted);
    Assert.AreEqual(2, data.Products.Count);
}
    </sampleCode>
  </item>
</examples>